<?php
class Default_Models_LoginModel{
	static function login($arrParam){
		$db = Zend_Db_Table::getDefaultAdapter();
	   	$auth = Sunnet_Auth_User::getInstance();
	
	   	$authAdapter = new Zend_Auth_Adapter_DbTable($db);
	    $authAdapter->setTableName('user')
	    			->setIdentityColumn('username')
	    			->setCredentialColumn('password');
	    $uname = trim($arrParam['username']);
	    $password = trim($arrParam['password']);
		if($uname == "" || $password == ""){
			return false;
		}
		else{
		    $paswd = sha1($password,false);
    		$authAdapter->setIdentity($uname);
    		$authAdapter->setCredential($paswd);
    		$select = $authAdapter->getDbSelect();
    		$select->where('status = 1');	
    		$select->where('active = 1');	
			$result = $auth->authenticate($authAdapter);
    		if($result->isValid()){
    			$data = $authAdapter->getResultRowObject(null,array('password'));
    			$auth->getStorage()->write($data);

	            // If "remember" was marked
	            if ($arrParam['remember']) {
	                Zend_Session::rememberMe();
	            }
                Zend_Session::start();
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
	}
}